$(function(){

	var selectoptions=[
		{find: '.ui-icon-alert'                  ,icon: 'ui-icon-alert'                  },
		{find: '.ui-icon-arrow-1-e'              ,icon: 'ui-icon-arrow-1-e'              },
		{find: '.ui-icon-arrow-1-n'              ,icon: 'ui-icon-arrow-1-n'              },
		{find: '.ui-icon-arrow-1-ne'             ,icon: 'ui-icon-arrow-1-ne'             },
		{find: '.ui-icon-arrow-1-nw'             ,icon: 'ui-icon-arrow-1-nw'             },
		{find: '.ui-icon-arrow-1-s'              ,icon: 'ui-icon-arrow-1-s'              },
		{find: '.ui-icon-arrow-1-se'             ,icon: 'ui-icon-arrow-1-se'             },
		{find: '.ui-icon-arrow-1-sw'             ,icon: 'ui-icon-arrow-1-sw'             },
		{find: '.ui-icon-arrow-1-w'              ,icon: 'ui-icon-arrow-1-w'              },
		{find: '.ui-icon-arrow-2-e-w'            ,icon: 'ui-icon-arrow-2-e-w'            },
		{find: '.ui-icon-arrow-2-n-s'            ,icon: 'ui-icon-arrow-2-n-s'            },
		{find: '.ui-icon-arrow-2-ne-sw'          ,icon: 'ui-icon-arrow-2-ne-sw'          },
		{find: '.ui-icon-arrow-2-se-nw'          ,icon: 'ui-icon-arrow-2-se-nw'          },
		{find: '.ui-icon-arrow-4'                ,icon: 'ui-icon-arrow-4'                },
		{find: '.ui-icon-arrow-4-diag'           ,icon: 'ui-icon-arrow-4-diag'           },
		{find: '.ui-icon-arrowrefresh-1-e'       ,icon: 'ui-icon-arrowrefresh-1-e'       },
		{find: '.ui-icon-arrowrefresh-1-n'       ,icon: 'ui-icon-arrowrefresh-1-n'       },
		{find: '.ui-icon-arrowrefresh-1-s'       ,icon: 'ui-icon-arrowrefresh-1-s'       },
		{find: '.ui-icon-arrowrefresh-1-w'       ,icon: 'ui-icon-arrowrefresh-1-w'       },
		{find: '.ui-icon-arrowreturn-1-e'        ,icon: 'ui-icon-arrowreturn-1-e'        },
		{find: '.ui-icon-arrowreturn-1-n'        ,icon: 'ui-icon-arrowreturn-1-n'        },
		{find: '.ui-icon-arrowreturn-1-s'        ,icon: 'ui-icon-arrowreturn-1-s'        },
		{find: '.ui-icon-arrowreturn-1-w'        ,icon: 'ui-icon-arrowreturn-1-w'        },
		{find: '.ui-icon-arrowreturnthick-1-e'   ,icon: 'ui-icon-arrowreturnthick-1-e'   },
		{find: '.ui-icon-arrowreturnthick-1-n'   ,icon: 'ui-icon-arrowreturnthick-1-n'   },
		{find: '.ui-icon-arrowreturnthick-1-s'   ,icon: 'ui-icon-arrowreturnthick-1-s'   },
		{find: '.ui-icon-arrowreturnthick-1-w'   ,icon: 'ui-icon-arrowreturnthick-1-w'   },
		{find: '.ui-icon-arrowstop-1-e'          ,icon: 'ui-icon-arrowstop-1-e'          },
		{find: '.ui-icon-arrowstop-1-n'          ,icon: 'ui-icon-arrowstop-1-n'          },
		{find: '.ui-icon-arrowstop-1-s'          ,icon: 'ui-icon-arrowstop-1-s'          },
		{find: '.ui-icon-arrowstop-1-w'          ,icon: 'ui-icon-arrowstop-1-w'          },
		{find: '.ui-icon-arrowthick-1-e'         ,icon: 'ui-icon-arrowthick-1-e'         },
		{find: '.ui-icon-arrowthick-1-n'         ,icon: 'ui-icon-arrowthick-1-n'         },
		{find: '.ui-icon-arrowthick-1-nw'        ,icon: 'ui-icon-arrowthick-1-nw'        },
		{find: '.ui-icon-arrowthick-1-s'         ,icon: 'ui-icon-arrowthick-1-s'         },
		{find: '.ui-icon-arrowthick-1-se'        ,icon: 'ui-icon-arrowthick-1-se'        },
		{find: '.ui-icon-arrowthick-1-sw'        ,icon: 'ui-icon-arrowthick-1-sw'        },
		{find: '.ui-icon-arrowthick-1-w'         ,icon: 'ui-icon-arrowthick-1-w'         },
		{find: '.ui-icon-arrowthick-2-e-w'       ,icon: 'ui-icon-arrowthick-2-e-w'       },
		{find: '.ui-icon-arrowthick-2-n-s'       ,icon: 'ui-icon-arrowthick-2-n-s'       },
		{find: '.ui-icon-arrowthick-2-ne-sw'     ,icon: 'ui-icon-arrowthick-2-ne-sw'     },
		{find: '.ui-icon-arrowthick-2-se-nw'     ,icon: 'ui-icon-arrowthick-2-se-nw'     },
		{find: '.ui-icon-arrowthickstop-1-e'     ,icon: 'ui-icon-arrowthickstop-1-e'     },
		{find: '.ui-icon-arrowthickstop-1-n'     ,icon: 'ui-icon-arrowthickstop-1-n'     },
		{find: '.ui-icon-arrowthickstop-1-s'     ,icon: 'ui-icon-arrowthickstop-1-s'     },
		{find: '.ui-icon-arrowthickstop-1-w'     ,icon: 'ui-icon-arrowthickstop-1-w'     },
		{find: '.ui-icon-battery-0'              ,icon: 'ui-icon-battery-0'              },
		{find: '.ui-icon-battery-1'              ,icon: 'ui-icon-battery-1'              },
		{find: '.ui-icon-battery-2'              ,icon: 'ui-icon-battery-2'              },
		{find: '.ui-icon-battery-3'              ,icon: 'ui-icon-battery-3'              },
		{find: '.ui-icon-bookmark'               ,icon: 'ui-icon-bookmark'               },
		{find: '.ui-icon-bullet'                 ,icon: 'ui-icon-bullet'                 },
		{find: '.ui-icon-calculator'             ,icon: 'ui-icon-calculator'             },
		{find: '.ui-icon-calendar'               ,icon: 'ui-icon-calendar'               },
		{find: '.ui-icon-cancel'                 ,icon: 'ui-icon-cancel'                 },
		{find: '.ui-icon-carat-1-e'              ,icon: 'ui-icon-carat-1-e'              },
		{find: '.ui-icon-carat-1-n'              ,icon: 'ui-icon-carat-1-n'              },
		{find: '.ui-icon-carat-1-ne'             ,icon: 'ui-icon-carat-1-ne'             },
		{find: '.ui-icon-carat-1-nw'             ,icon: 'ui-icon-carat-1-nw'             },
		{find: '.ui-icon-carat-1-s'              ,icon: 'ui-icon-carat-1-s'              },
		{find: '.ui-icon-carat-1-se'             ,icon: 'ui-icon-carat-1-se'             },
		{find: '.ui-icon-carat-1-sw'             ,icon: 'ui-icon-carat-1-sw'             },
		{find: '.ui-icon-carat-1-w'              ,icon: 'ui-icon-carat-1-w'              },
		{find: '.ui-icon-carat-2-e-w'            ,icon: 'ui-icon-carat-2-e-w'            },
		{find: '.ui-icon-carat-2-n-s'            ,icon: 'ui-icon-carat-2-n-s'            },
		{find: '.ui-icon-cart'                   ,icon: 'ui-icon-cart'                   },
		{find: '.ui-icon-check'                  ,icon: 'ui-icon-check'                  },
		{find: '.ui-icon-circle-arrow-e'         ,icon: 'ui-icon-circle-arrow-e'         },
		{find: '.ui-icon-circle-arrow-n'         ,icon: 'ui-icon-circle-arrow-n'         },
		{find: '.ui-icon-circle-arrow-s'         ,icon: 'ui-icon-circle-arrow-s'         },
		{find: '.ui-icon-circle-arrow-w'         ,icon: 'ui-icon-circle-arrow-w'         },
		{find: '.ui-icon-circle-check'           ,icon: 'ui-icon-circle-check'           },
		{find: '.ui-icon-circle-close'           ,icon: 'ui-icon-circle-close'           },
		{find: '.ui-icon-circle-minus'           ,icon: 'ui-icon-circle-minus'           },
		{find: '.ui-icon-circle-plus'            ,icon: 'ui-icon-circle-plus'            },
		{find: '.ui-icon-circle-triangle-e'      ,icon: 'ui-icon-circle-triangle-e'      },
		{find: '.ui-icon-circle-triangle-n'      ,icon: 'ui-icon-circle-triangle-n'      },
		{find: '.ui-icon-circle-triangle-s'      ,icon: 'ui-icon-circle-triangle-s'      },
		{find: '.ui-icon-circle-triangle-w'      ,icon: 'ui-icon-circle-triangle-w'      },
		{find: '.ui-icon-circle-zoomin'          ,icon: 'ui-icon-circle-zoomin'          },
		{find: '.ui-icon-circle-zoomout'         ,icon: 'ui-icon-circle-zoomout'         },
		{find: '.ui-icon-circlesmall-close'      ,icon: 'ui-icon-circlesmall-close'      },
		{find: '.ui-icon-circlesmall-minus'      ,icon: 'ui-icon-circlesmall-minus'      },
		{find: '.ui-icon-circlesmall-plus'       ,icon: 'ui-icon-circlesmall-plus'       },
		{find: '.ui-icon-clipboard'              ,icon: 'ui-icon-clipboard'              },
		{find: '.ui-icon-clock'                  ,icon: 'ui-icon-clock'                  },
		{find: '.ui-icon-close'                  ,icon: 'ui-icon-close'                  },
		{find: '.ui-icon-closethick'             ,icon: 'ui-icon-closethick'             },
		{find: '.ui-icon-comment'                ,icon: 'ui-icon-comment'                },
		{find: '.ui-icon-contact'                ,icon: 'ui-icon-contact'                },
		{find: '.ui-icon-copy'                   ,icon: 'ui-icon-copy'                   },
		{find: '.ui-icon-disk'                   ,icon: 'ui-icon-disk'                   },
		{find: '.ui-icon-document'               ,icon: 'ui-icon-document'               },
		{find: '.ui-icon-document-b'             ,icon: 'ui-icon-document-b'             },
		{find: '.ui-icon-eject'                  ,icon: 'ui-icon-eject'                  },
		{find: '.ui-icon-extlink'                ,icon: 'ui-icon-extlink'                },
		{find: '.ui-icon-flag'                   ,icon: 'ui-icon-flag'                   },
		{find: '.ui-icon-folder-collapsed'       ,icon: 'ui-icon-folder-collapsed'       },
		{find: '.ui-icon-folder-open'            ,icon: 'ui-icon-folder-open'            },
		{find: '.ui-icon-gear'                   ,icon: 'ui-icon-gear'                   },
		{find: '.ui-icon-grip-diagonal-se'       ,icon: 'ui-icon-grip-diagonal-se'       },
		{find: '.ui-icon-grip-dotted-horizontal' ,icon: 'ui-icon-grip-dotted-horizontal' },
		{find: '.ui-icon-grip-dotted-vertical'   ,icon: 'ui-icon-grip-dotted-vertical'   },
		{find: '.ui-icon-grip-solid-horizontal'  ,icon: 'ui-icon-grip-solid-horizontal'  },
		{find: '.ui-icon-grip-solid-vertical'    ,icon: 'ui-icon-grip-solid-vertical'    },
		{find: '.ui-icon-gripsmall-diagonal-se'  ,icon: 'ui-icon-gripsmall-diagonal-se'  },
		{find: '.ui-icon-heart'                  ,icon: 'ui-icon-heart'                  },
		{find: '.ui-icon-help'                   ,icon: 'ui-icon-help'                   },
		{find: '.ui-icon-home'                   ,icon: 'ui-icon-home'                   },
		{find: '.ui-icon-image'                  ,icon: 'ui-icon-image'                  },
		{find: '.ui-icon-info'                   ,icon: 'ui-icon-info'                   },
		{find: '.ui-icon-key'                    ,icon: 'ui-icon-key'                    },
		{find: '.ui-icon-lightbulb'              ,icon: 'ui-icon-lightbulb'              },
		{find: '.ui-icon-link'                   ,icon: 'ui-icon-link'                   },
		{find: '.ui-icon-locked'                 ,icon: 'ui-icon-locked'                 },
		{find: '.ui-icon-mail-closed'            ,icon: 'ui-icon-mail-closed'            },
		{find: '.ui-icon-mail-open'              ,icon: 'ui-icon-mail-open'              },
		{find: '.ui-icon-minus'                  ,icon: 'ui-icon-minus'                  },
		{find: '.ui-icon-minusthick'             ,icon: 'ui-icon-minusthick'             },
		{find: '.ui-icon-newwin'                 ,icon: 'ui-icon-newwin'                 },
		{find: '.ui-icon-note'                   ,icon: 'ui-icon-note'                   },
		{find: '.ui-icon-notice'                 ,icon: 'ui-icon-notice'                 },
		{find: '.ui-icon-pause'                  ,icon: 'ui-icon-pause'                  },
		{find: '.ui-icon-pencil'                 ,icon: 'ui-icon-pencil'                 },
		{find: '.ui-icon-person'                 ,icon: 'ui-icon-person'                 },
		{find: '.ui-icon-pin-s'                  ,icon: 'ui-icon-pin-s'                  },
		{find: '.ui-icon-pin-w'                  ,icon: 'ui-icon-pin-w'                  },
		{find: '.ui-icon-play'                   ,icon: 'ui-icon-play'                   },
		{find: '.ui-icon-plus'                   ,icon: 'ui-icon-plus'                   },
		{find: '.ui-icon-plusthick'              ,icon: 'ui-icon-plusthick'              },
		{find: '.ui-icon-power'                  ,icon: 'ui-icon-power'                  },
		{find: '.ui-icon-print'                  ,icon: 'ui-icon-print'                  },
		{find: '.ui-icon-radio-off'              ,icon: 'ui-icon-radio-off'              },
		{find: '.ui-icon-radio-on'               ,icon: 'ui-icon-radio-on'               },
		{find: '.ui-icon-refresh'                ,icon: 'ui-icon-refresh'                },
		{find: '.ui-icon-scissors'               ,icon: 'ui-icon-scissors'               },
		{find: '.ui-icon-script'                 ,icon: 'ui-icon-script'                 },
		{find: '.ui-icon-search'                 ,icon: 'ui-icon-search'                 },
		{find: '.ui-icon-seek-end'               ,icon: 'ui-icon-seek-end'               },
		{find: '.ui-icon-seek-first'             ,icon: 'ui-icon-seek-first'             },
		{find: '.ui-icon-seek-next'              ,icon: 'ui-icon-seek-next'              },
		{find: '.ui-icon-seek-prev'              ,icon: 'ui-icon-seek-prev'              },
		{find: '.ui-icon-seek-start'             ,icon: 'ui-icon-seek-start'             },
		{find: '.ui-icon-shuffle'                ,icon: 'ui-icon-shuffle'                },
		{find: '.ui-icon-signal'                 ,icon: 'ui-icon-signal'                 },
		{find: '.ui-icon-signal-diag'            ,icon: 'ui-icon-signal-diag'            },
		{find: '.ui-icon-squaresmall-close'      ,icon: 'ui-icon-squaresmall-close'      },
		{find: '.ui-icon-squaresmall-minus'      ,icon: 'ui-icon-squaresmall-minus'      },
		{find: '.ui-icon-squaresmall-plus'       ,icon: 'ui-icon-squaresmall-plus'       },
		{find: '.ui-icon-star'                   ,icon: 'ui-icon-star'                   },
		{find: '.ui-icon-stop'                   ,icon: 'ui-icon-stop'                   },
		{find: '.ui-icon-suitcase'               ,icon: 'ui-icon-suitcase'               },
		{find: '.ui-icon-tag'                    ,icon: 'ui-icon-tag'                    },
		{find: '.ui-icon-transfer-e-w'           ,icon: 'ui-icon-transfer-e-w'           },
		{find: '.ui-icon-transferthick-e-w'      ,icon: 'ui-icon-transferthick-e-w'      },
		{find: '.ui-icon-trash'                  ,icon: 'ui-icon-trash'                  },
		{find: '.ui-icon-triangle-1-e'           ,icon: 'ui-icon-triangle-1-e'           },
		{find: '.ui-icon-triangle-1-n'           ,icon: 'ui-icon-triangle-1-n'           },
		{find: '.ui-icon-triangle-1-ne'          ,icon: 'ui-icon-triangle-1-ne'          },
		{find: '.ui-icon-triangle-1-nw'          ,icon: 'ui-icon-triangle-1-nw'          },
		{find: '.ui-icon-triangle-1-s'           ,icon: 'ui-icon-triangle-1-s'           },
		{find: '.ui-icon-triangle-1-se'          ,icon: 'ui-icon-triangle-1-se'          },
		{find: '.ui-icon-triangle-1-sw'          ,icon: 'ui-icon-triangle-1-sw'          },
		{find: '.ui-icon-triangle-1-w'           ,icon: 'ui-icon-triangle-1-w'           },
		{find: '.ui-icon-triangle-2-e-w'         ,icon: 'ui-icon-triangle-2-e-w'         },
		{find: '.ui-icon-triangle-2-n-s'         ,icon: 'ui-icon-triangle-2-n-s'         },
		{find: '.ui-icon-unlocked'               ,icon: 'ui-icon-unlocked'               },
		{find: '.ui-icon-video'                  ,icon: 'ui-icon-video'                  },
		{find: '.ui-icon-volume-off'             ,icon: 'ui-icon-volume-off'             },
		{find: '.ui-icon-volume-on'              ,icon: 'ui-icon-volume-on'              },
		{find: '.ui-icon-wrench'                 ,icon: 'ui-icon-wrench'                 },
		{find: '.ui-icon-zoomin'                 ,icon: 'ui-icon-zoomin'                 },
		{find: '.ui-icon-zoomout'                ,icon: 'ui-icon-zoomout'                }
	];

    $("#enviar").button({ icons:{primary:"ui-icon-transferthick-e-w"} });
    $("#reset").button({ icons:{primary:"ui-icon-refresh"} });

    jQuery("#filtro-menu").jqGrid(
        {
	    url:'<?php echo site_url($this->url.'grid') ?>',
	    datatype: "json",
            colNames:[
                '<?php echo $this->lang->line('G_ID') ?>',
                '<?php echo $this->lang->line('G_NOMBRE') ?>',
                '<?php echo $this->lang->line('G_URL_TAB_CONTENT') ?>',
                '<?php echo $this->lang->line('G_PARENT') ?>',
                '<?php echo $this->lang->line('G_ORDER') ?>',
                '<?php echo $this->lang->line('G_MENU_ICON') ?>',
                '<?php echo $this->lang->line('G_TAB_INDEX') ?>',
                '<?php echo $this->lang->line('G_HREF') ?>',
                '<?php echo $this->lang->line('G_CONTROLLER') ?>'
            ],
	    colModel:[
	        {
		    name :'id',
		    index:'id',
		    width:55,
		    key  :true
	        },
	        {
		    name :'nombre',
		    index:'nombre',
		    width:55
	        },
	        {
		    name :'url_tab_content',
		    index:'url_tab_content',
		    width:55
	        },
	        {
		    name :'parent',
		    index:'parent',
		    width:55,
	        },
	        {
		    name :'order',
		    index:'order',
		    width:55,
	        },
	        {
		    name :'menu_icon',
		    index:'menu_icon',
		    width:55,
	        },
	        {
		    name :'tab_index',
		    index:'tab_index',
		    width:55,
	        },
	        {
		    name :'href',
		    index:'href',
		    width:55,
	        },
	        {
		    name :'controller',
		    index:'controller',
		    width:55,
	        }
	    ],
	    rowNum: 10,
	    width: 700,
	    rowList: [10,20,30],
	    pager: '#barra-menu',
	    sortname: 'parent',
	    viewrecords: true,
	    sortorder: "asc",
	    caption: "<?php echo $this->lang->line('G_CAPTION') ?>",
	    height: '100%',
		multiselect: true,
		forceFit: true,
		rownumbers: true,
		sortable: true,
		ondblClickRow:
			function(rowid,iCol, cellcontent,e){
				//var rowsSelected = $("table#filtro-menu").getGridParam('iRow');
				var rowsSelected=rowid;
				var bodyForm = $('form', 'div#dialog-form-menu').html();
				$("table#filtro-menu").jqGrid('getDialogsForms', 'edit', rowsSelected, bodyForm, "<?php echo $this->lang->line('FORM_EDIT_TITLE') ?>");
				$("select#menu_icon", "form[id^='form-edit__filtro-menu']").selectmenu({
					width: 300,
					style:"popup",
					icons: selectoptions
				});
			},
			onRightClickRow:function(rowid, iRow, iCol, e){
				crea_contextmenu('#myMenu',e);
				$('#myMenu a').click(function(){
					este=$(this);
					nombre=este.attr('name');
					switch(nombre){
						case 'edit':{
							var rowsSelected=rowid;
							var bodyForm = $('form', 'div#dialog-form-menu').html();
							$("table#filtro-menu").jqGrid('getDialogsForms', 'edit', rowsSelected, bodyForm, "<?php echo $this->lang->line('FORM_EDIT_TITLE') ?>");
							$("select#menu_icon", "form[id^='form-edit__filtro-menu']").selectmenu({
								width: 300,
								style:"popup",
								icons: selectoptions
							});
						}
						break;
						case 'view':{
							jQuery("table#filtro-menu").viewGridRow( rowid );
						}
						break;
					}
					//window.console.log(nombre);
					$('#myMenu').hide();
					return false;
				});
			}
        }
    );

    jQuery("#filtro-menu").jqGrid(
        'navGrid',
        '#barra-menu',
        {
	    edit:false,
	    add :false,
	    del :false,
	    view:true
        },
        {},
        {},
        {},
        {
	    multipleSearch:true,
	    multipleGroup :true,
	    showQuery     :true
        }
    ).jqGrid(
        'navButtonAdd',
        "#barra-menu",
        {
            caption:"",
            buttonicon:"ui-icon-pencil",
            onClickButton: function(){
                var rowsSelected = $("table#filtro-menu").getGridParam('selarrrow');
                var bodyForm = $('form', 'div#dialog-form-menu').html();
                $("table#filtro-menu").jqGrid('getDialogsForms', 'edit', rowsSelected, bodyForm, "<?php echo $this->lang->line('FORM_EDIT_TITLE') ?>");
                $("select#menu_icon", "form[id^='form-edit__filtro-menu']").selectmenu({
                    width: 300,
                    style:"popup",
                    icons: selectoptions
                });
            },
            position:"first"
        }
    ).jqGrid(
        'navButtonAdd',
        "#barra-menu",
        {
            caption:"",
            buttonicon:"ui-icon-plus",
            onClickButton: function(){
                var bodyForm = $('form', 'div#dialog-form-menu').html();
                $("table#filtro-menu").jqGrid('getDialogsForms', 'add', '', bodyForm, "<?php echo $this->lang->line('FORM_ADD_TITLE') ?>");
                $("select#menu_icon", "form[id^='form-add__filtro-menu']").selectmenu({
                    width: 300,
                    style:"popup",
                    icons: selectoptions
                });
            },
            position:"first"
        }
    ).jqGrid(
        'sortableRows'
    ).jqGrid(
        'gridResize'
    );


	$('button[id="enviar"]').live('click',function(){
		enviar =$(this);
		formid =enviar.offsetParent().attr("id");
		campos =['id','nombre','url_tab_content','parent','order','menu_icon','tab_index','href','controller'];
		data   =prepara_post(enviar,campos,formid);
		resp   =hace_post('<?php echo site_url($this->url."guardar") ?>',data,false);
		muestra_msj(formid,campos,resp);
	});
})
